package demo2;

import org.apache.zookeeper.KeeperException;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;

/**
 * --------神兽出没--------/
 *   ┌─┐     ┌─┐
 * ┌─┘─┴─────┘─┴─┐
 * │      ─      │
 * │  ┬─┘   └─┬  │
 * │             │
 * │      ┴      │  Code is far away from bug with the animal protecting
 * └───┐      ┌──┘  神兽保佑,代码无bug
 *     │      │
 *     │      └──────┐
 *     │             ├┐
 *     │             ┌┘
 *     └┐ ┐ ┌───┬─┐ ┌┘
 *      │ ┤ ┤   │ ┤ ┤
 *      └─┴─┘   └─┴─┘
 * --------感觉萌萌哒--------/
 *
 * @author wuyoushan
 * @date 2018/5/3
 */
public class ClientTest {

    public static void main(String[] args) throws IOException,
            InterruptedException, KeeperException {
        BaseZookeeper baseZookeeper = new BaseZookeeper();

        String host = "127.0.0.1:2181";

        // 连接zookeeper
        baseZookeeper.connectZookeeper(host);
        System.out.println("--------connect zookeeper ok-----------");

        // 创建节点
        byte[] data = { 1, 2, 3, 4, 5 };
        if(null==baseZookeeper.existsNode("/test")){
            String result = baseZookeeper.createNode("/test", data);
            System.out.println(result);
            System.out.println("--------create node ok-----------");
        }

        // 获取某路径下所有节点
        List<String> children = baseZookeeper.getChildren("/");
        for (String child : children) {
            System.out.println(child);
        }
        System.out.println("--------get children ok-----------");

        // 获取节点数据
        byte[] nodeData = baseZookeeper.getData("/test");
        System.out.println(Arrays.toString(nodeData));
//		System.out.println(new String(nodeData));
        System.out.println("--------get node data ok-----------");

        // 更新节点数据
        data = "test data".getBytes();
        baseZookeeper.setData("/test", data, -1);
        System.out.println("--------set node data ok-----------");

        byte[] nodeData1 = baseZookeeper.getData("/test");
//		System.out.println(Arrays.toString(nodeData1));
        System.out.println(new String(nodeData1));
        System.out.println("--------get node new data ok-----------");

        baseZookeeper.deletNode("/test", -1);
        System.out.println("删除节点成功");

        baseZookeeper.closeConnect();
        System.out.println("--------close zookeeper ok-----------");
    }
}
